package admin

import (
	"beego-demo/controllers/base"
	"beego-demo/models"
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
)

type UserController struct {
	beego.Controller
}

type CheckUserInfo struct {
	Id    int         `form:"id"`
	Name  interface{} `form:"username"`
	Age   int         `form:"age"`
	Email string      `form:"email"`
}

type CheckUserInfoJson struct {
	Id    int         `json:"id"`
	Name  interface{} `json:"username"`
	Age   int         `json:"age"`
	Email string      `json:"email"`
}

func (c *UserController) Get() {
	c.Data["Username"] = "ChinaYin"
	c.Data["HomePage"] = "https://gitee.com/zy-explorej2ee"
	c.TplName = "admin/userinfo.tpl"
}

// Post restful风格API
func (c *UserController) Post() {
	c.EnableXSRF = false
	param1 := c.Ctx.Input.Param(":username")
	param2 := c.Ctx.Input.Param(":password")
	fmt.Printf("入参: %v, %v\n", param1, param2)
	c.Ctx.WriteString("hello admin")
}

//CheckUserInfo 接收form-data请求参数
func (c *UserController) CheckUserInfo() {
	c.EnableXSRF = false
	u := CheckUserInfo{}
	if err := c.ParseForm(&u); err != nil {
		c.Data["json"] = base.ParamError(nil)
		c.ServeJSON()
		return
	}
	logs.Info("入参：user=%v\n", u)
	logs.Info("用户ID=%v, 用户名称=%v,用户年龄=%v,用户邮箱=%v\n", u.Id, u.Name, u.Age, u.Email)
	c.Data["json"] = base.SuccessJson(u)
	c.ServeJSON()
}

// CheckUserInfoJson 接收json请求参数
func (c *UserController) CheckUserInfoJson() {
	c.EnableXSRF = false
	u := CheckUserInfoJson{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &u); err != nil {
		logs.Error(err)
		c.Data["json"] = base.ParamError(nil)
		c.ServeJSON()
		return
	}
	logs.Info("入参：user=%v\n", u)
	logs.Info("用户ID=%v, 用户名称=%v,用户年龄=%v,用户邮箱=%v\n", u.Id, u.Name, u.Age, u.Email)
	c.Data["json"] = base.SuccessJson(u)
	c.ServeJSON()
}

// AddSysUser 新增用户
func (c *UserController) AddSysUser() {
	u := models.SysUser{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &u); err != nil {
		logs.Error(err)
		c.Data["json"] = base.ParamError(nil)
		c.ServeJSON()
		return
	}
	user := models.AddSysUser(u)
	c.Data["json"] = base.SuccessJson(user)
	c.ServeJSON()
}

// RemoveSysUser 删除用户
func (c *UserController) RemoveSysUser() {
	userId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
	if err != nil {
		logs.Error("删除用户异常：", err)
		c.Data["json"] = base.ParamError(nil)
		c.ServeJSON()
		return
	}
	user := models.RemoveSysUser(userId)
	c.Data["json"] = base.SuccessJson(user)
	c.ServeJSON()
}

// UpdateSysUser 新增用户
func (c *UserController) UpdateSysUser() {
	u := models.SysUser{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &u); err != nil {
		logs.Error(err)
		c.Data["json"] = base.ParamError(nil)
		c.ServeJSON()
		return
	}
	user := models.UpdateSysUser(u)
	c.Data["json"] = base.SuccessJson(user)
	c.ServeJSON()
}

// GetUserById 根据ID查询用户信息
func (c *UserController) GetUserById() {
	userId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
	if err != nil {
		logs.Error(err)
		c.Data["json"] = base.ParamError(nil)
		c.ServeJSON()
		return
	}
	user, err := models.GetSysUserById(userId)
	if err != nil {
		logs.Error(err)
		c.Data["json"] = base.CustomError(80801001, "该用户不存在")
		c.ServeJSON()
		return
	}
	userProfile, err := models.GetUserProfileById(user.Profile.Id)
	if err != nil {
		c.Data["json"] = base.CustomError(80801002, "该用户详情不存在")
		c.ServeJSON()
		return
	}
	user.Profile = userProfile
	c.Data["json"] = base.SuccessJson(user)
	c.ServeJSON()
}

// GetUserList 查询所有用户
func (c *UserController) GetUserList() {
	list, err := models.GetSysUserList()
	if err != nil {
		logs.Error("查询所有用户出现异常", err)
		c.Data["json"] = base.CustomError(80801005, "查询所有用户出现异常")
		c.ServeJSON()
		return
	}
	for _, user := range list {
		userProfile, err := models.GetUserProfileById(user.Profile.Id)
		if err != nil {
			logs.Error("查询用户详情异常：", err)
		} else {
			user.Profile = userProfile
		}
	}
	c.Data["json"] = base.SuccessJson(list)
	c.ServeJSON()
}
